#include <QPainter>
#include <math.h>
#include "Draw/drawdiagram.h"

#include "UGUI/ugui.h"
#include "LCD/lcd.h"
#include "Curve/Curve.h"

extern void Bar_Palette_Init(const uint8_t scale[], const uint8_t scale_len, uint32_t bar[128], int16_t &bar_len);

static const uint8_t scale[] = {0, 64, 128, 192, 255};
DrawDiagram::DrawDiagram(QWidget* const widget, QObject *parent)
    : QObject{parent}, Widget(widget)
{
    const struct draw_pixel centre = {.y=(MATRIX_SIZE+1)/2, .x=(MATRIX_SIZE)/2};
    struct draw_pixel pixel;
    double angle;
    double radius;
    int16_t Angle;
    Bar_Cnt=0;
    Bar_Palette_Init(scale, sizeof(scale), bar, bar_len);
    for(int y=0; y<MATRIX_SIZE; y++)
    {
        for(int x=0; x<MATRIX_SIZE; x++)
        {
            pixel.x = x-centre.x;
            pixel.y = MATRIX_SIZE-y-centre.y;
            angle  = atan2f(pixel.y, pixel.x) * 180 / 3.14159265359;
            radius = sqrt(pow(pixel.x, 2) + pow(pixel.y, 2));//+0.7f;
            Angle = angle;
            if(Angle<0) Angle = 360 + Angle;
            matrix[y][x].angle = Angle/6;
            matrix[y][x].radius = radius;
        }
    }
#if 0
    printf("matrix[%d][%d] = {\r\n", MATRIX_SIZE, MATRIX_SIZE);
    for(int y=0; y<MATRIX_SIZE; y++)
    {
        printf("{");
        for(int x=0; x<MATRIX_SIZE; x++)
        {
            printf("%4d, ",  matrix[y][x].angle);
            //printf("(%2d,%2d), ",  x, y);
            //printf("(%3d,%3d), ",  x-centre.x, y-centre.y);
        }
        printf("},\r\n");
    }
    printf("};\r\n");
    printf("matrix[%d][%d] = {\r\n", MATRIX_SIZE, MATRIX_SIZE);
    for(int y=0; y<MATRIX_SIZE; y++)
    {
        printf("{");
        for(int x=0; x<MATRIX_SIZE; x++)
        {
            //printf("%4d, ",  matrix[y][x].angle);
            //printf("(%2d,%2d), ",  x, y);
            printf("(%3d,%3d), ",  matrix[y][x].x, matrix[y][x].y);
        }
        printf("},\r\n");
    }
    printf("};\r\n");
    printf("matrix[%d][%d] = {\r\n", MATRIX_SIZE, MATRIX_SIZE);
    for(int y=0; y<MATRIX_SIZE; y++)
    {
        printf("{");
        for(int x=0; x<MATRIX_SIZE; x++)
        {
            printf("%4d, ",  matrix[y][x].radius);
            //printf("(%2d,%2d), ",  x, y);
            //printf("(%3d,%3d), ",  x-centre.x, y-centre.y);
        }
        printf("},\r\n");
    }
    printf("};\r\n");
#else
    // 螺旋效果数据输出
    /*
    printf("matrix[%d][%d] = {\r\n", MATRIX_SIZE, MATRIX_SIZE);
    for(int y=0; y<MATRIX_SIZE; y++)
    {
        printf("{");
        for(int x=0; x<MATRIX_SIZE; x++)
        {
            printf("%4d, ",  (int)matrix[y][x].angle);
            //printf("0x%04X, ",  matrix[y][x].pixel);
        }
        printf("},\r\n");
    }
    printf("};\r\n");
    */
#endif
}

void DrawDiagram::get_draw_square_map()
{
    square1(41, 24);
    printf(" const uint8_t draw_square_map[41][41] = { \r\n");
    for(int index=0; index<41; index++)
    {
        printf("{");
        for(int i=0; i<41; i++)
        {
            printf("0x%02X, ", ugui_get_point(index, i));
        }
        printf("}, \r\n");
    }
    printf("}; \r\n");
    printf("[%s--%d] \r\n", __func__, __LINE__);
    fflush(stderr);
    //QThread::msleep(100);
    exit(0);
}

void DrawDiagram::gridding(uint16_t row, uint16_t col)
{
    // 窗口大小
    const int _width=Widget->width();
    const int _height=Widget->height();

    if(row<1) row=1;
    if(col<1) col=1;
    const int _ww = _width/col;
    const int _hh = _height/row;
    qDebug("[%s--%d] _width:[%d %d %d] _high:[%d %d %d] \r\n", __func__, __LINE__, _width, _ww, col, _height, _hh, row);
    int i;
    QPainter painter(Widget);
    painter.setPen(QPen(QColor(11,67,127),8));
    painter.setRenderHint(QPainter:: Antialiasing, true);  //设置渲染,启动反锯齿
    //QSize ViewWH(100,100);        //定义视口宽高
    QSize ViewWH(_width, _height);        //定义视口宽高
    painter.setViewport(0, 0,ViewWH.width(),ViewWH.height());
    //painter.setWindow(-50,50,ViewWH.width(),-ViewWH.height());
    painter.setWindow(-50,0,ViewWH.width(),-ViewWH.height());
    painter.setBrush(QColor(187,229,253));
    painter.setPen(QPen(QColor(11,67,127),2));
    painter.drawRect(-50,0,ViewWH.width(),-ViewWH.height()); //窗口背景
    painter.drawLine(-50,-50,ViewWH.width(),-50);       //画X坐标
    painter.drawLine(-50,-98,ViewWH.width(),-98);       //画X坐标
    //painter.drawLine(-50,_hh,_width,_hh);       //画X坐标
    //painter.drawLine(-50,_hh*2,_width,_hh*2);       //画X坐标
    for(i=0; i<row; i++)
    {
        painter.drawLine(-50,-_hh*i,_width,-_hh*i);       //画X坐标
    }
    //painter.drawLine(0,-50,0,50);           //画Y坐标
    //painter.drawLine(70,-50,70,50);           //画Y坐标
    //painter.drawLine(10,-50,10,30);           //画Y坐标
    for(i=0; i<(col-1); i++) painter.drawLine(_ww*i,-ViewWH.height(),_ww*i,ViewWH.height());
}

void DrawDiagram::square1(const int _wh, uint16_t divide)
{
    //uint32_t color;//, color_tmp;
//    if(num<1) num=1;
//    const int _ww = _width/num;
//    const int _hh = _height/num;
    //uint16_t hh=0;
    uint16_t cnt;
    //timeout();
    qDebug("[%s--%d] _wh:%d divide:%d\r\n", __func__, __LINE__, _wh, divide);
    int i;//, index;
    // 绘制延长线
    const float pi = 3.1415926;
    const float circum = 3.1415926f*2;
    //const float Degree = 2*pi/divide + 0.01745f;   // pi/180=0.01745
    const float Degree = 2*pi/divide + 0.01f;   // pi/180=0.01745
    float degree;
    float angle = 2*pi/divide;
    float radii = _wh/1.999f;
    int Radii = (_wh+1)/2;

    float py=0.0f;
    float px=0.0f;
    float ph=0.0f;
    float pw=0.0f;
    //hh=(_height*1)/num;
    qDebug("\r\n[%s--%d] bar_len:%d ", __func__, __LINE__, bar_len);
    for(degree=0.0f; degree<Degree; degree +=0.01745f )
    {
        cnt=0;//Bar_Cnt;
        for(i=0; i<divide; i++)
        {
            // 根据三角函数计算出以圆心为原点的等分圆的x,y坐标
            angle = (circum*i)/divide+degree;
            //qDebug("[%s--%d] divide:%d angle:%f circum:%f", __func__, __LINE__, divide, angle, circum);
            py = qSin(angle)*radii;
            px = qCos(angle)*radii;
            // 根据等比公式计算与大正方形的切线的高 ==> py/ph=px/radii ==> ph= py*radii/px
            ph = (py*radii)/px;
            // 根据等比公式计算与大正方形的切线的高 ==> pw/radii=px/py ==> pw= px*radii/py
            pw = (px*radii)/py;
            // -45°~+45°, <45 || > 315
            if(((circum-pi/4)<angle) || (angle<(pi/4)))
            {
                //painter.drawLine(_height, -(Radii-(int)ph), Radii, -Radii);           //画Y坐标
                //UG_DrawLine(_wh, (Radii-(int)ph), Radii, Radii, bar[cnt]);
                UG_DrawLine(_wh, (Radii-(int)ph), Radii, Radii, cnt);
                //OLED_DrawLine(_height/hh+1, (Radii-(int)ph)/hh, Radii/hh-1, Radii/hh+1, 1);
                //if((circum-pi/4)<angle)
                //{
                    //qDebug("[%s--%d] (%3d, %4d) (%3d, %4d)", __func__, __LINE__, _height, -(Radii-(int)ph), Radii, -Radii);
                    //qDebug("[%s--%d] (%3d, %4d) (%3d, %4d)", __func__, __LINE__, _height/hh+1, (Radii-(int)ph)/hh, Radii/hh-1, Radii/hh+1);
                //}
            }
            // +45°~+135°, >45 && < 315
            // NO.3 --------------
            if(((pi/4)<=angle) && (angle<(3*pi/4)))
            {
                //painter.drawLine(Radii+(int)pw, 0, Radii, -Radii);           //画Y坐标
                //UG_DrawLine((Radii+(int)pw), 0, Radii, Radii, bar[cnt]);
                UG_DrawLine((Radii+(int)pw), 0, Radii, Radii, cnt);
            }
            // +135°~+225°, >135 && < 225
            // NO.7 -------------
            if(((3*pi/4)<=angle) && (angle<(5*pi/4)))
            {
                //painter.drawLine(0, -(Radii+(int)ph), Radii, -Radii);           //画Y坐标
                UG_DrawLine(0, (Radii+(int)ph), Radii, Radii, cnt);
                //qDebug("[%s--%d] (%3d, %4d) (%3d, %4d)", __func__, __LINE__, 0, (Radii+(int)ph)/hh, Radii/hh+1, Radii/hh);
            }
            // +225°~+315°, >225 && < 315
            // NO.12 ------------------------
            if(((5*pi/4)<=angle) && (angle<(7*pi/4)))
            {
                //painter.drawLine(Radii-(int)pw, -_height, Radii, -Radii);           //画Y坐标
                UG_DrawLine((Radii-(int)pw), _wh, Radii, Radii, cnt);
                //qDebug("[%s--%d] (%3d, %4d) (%3d, %4d)", __func__, __LINE__, (Radii-(int)pw)/hh, _height/hh+1, Radii/hh, Radii/hh);
            }
            cnt++;
            if(cnt>=bar_len) cnt=0;
            //if(cnt<0) cnt=bar_len-1;
            //qDebug("[%s--%d] Radii:%d angle:%f (%f, %f) ph:%f %d %d", __func__, __LINE__, Radii, angle, px, py, ph, -(Radii-(int)ph), -(Radii-(int)py));
        }
    }
}

void DrawDiagram::square2(uint16_t num, uint16_t divide)
{
    // 窗口大小
    const int _width=Widget->width();
    const int _height=Widget->height();

    //uint32_t color, color_tmp;
    if(num<1) num=1;
    const int _ww = _width/num;
    const int _hh = _height/num;
    uint16_t hh=0;
    uint16_t cnt;
    //timeout();
    qDebug("[%s--%d] _width:[%d %d %d] _high:[%d %d %d] \r\n", __func__, __LINE__, _width, _ww, num, _height, _hh, num);
    int i;//, index;
    QPainter painter(Widget);
    painter.setPen(QPen(QColor(11,67,127),1));
    painter.setRenderHint(QPainter:: Antialiasing, true);  //设置渲染,启动反锯齿
    //QSize ViewWH(100,100);        //定义视口宽高
    QSize ViewWH(_height, _height);        //定义视口宽高
    painter.setViewport(0, 0,ViewWH.width(),ViewWH.height());
    //painter.setWindow(-50,50,ViewWH.width(),-ViewWH.height());
    painter.setWindow(-(_width/2-(_height/2)),0,ViewWH.width(),-ViewWH.height());
    painter.setBrush(QColor(187,229,253));
    painter.setPen(QPen(QColor(11,67,127),1));
    //painter.drawRect(-50,0,ViewWH.width(),-ViewWH.height()); //窗口背景
    //painter.drawRect(0,0,ViewWH.width(),-ViewWH.height()); //窗口背景
    painter.drawRect(_height/2-_hh*10, -_height/2+_hh*2+10,_hh*21,-_hh*6); //窗口背景
    //painter.drawLine(-50,-50,ViewWH.width(),-50);       //画X坐标
    //painter.drawLine(-50,-98,ViewWH.width(),-98);       //画X坐标
    //painter.drawLine(-50,_hh,_width,_hh);       //画X坐标
    //painter.drawLine(-50,_hh*2,_width,_hh*2);       //画X坐标
    for(i=0; i<num+1; i++)
    {
        hh=(_height*i)/num;
        painter.drawLine(0,-hh,_height,-hh);       //画X坐标
    }
    //painter.drawLine(0,-50,0,50);           //画Y坐标
    //painter.drawLine(70,-50,70,50);           //画Y坐标
    //painter.drawLine(10,-50,10,30);           //画Y坐标
    for(i=0; i<num+1; i++)
    {
        hh=(_height*i)/num;
        painter.drawLine(hh,-ViewWH.height(),hh,ViewWH.height());           //画Y坐标
    }
    painter.setPen(QPen(QColor(255, 0, 0), 3));
    //painter.drawLine(0, 0, (_height/2), -(_height/2));           //画Y坐标
    painter.drawLine(0, 0, _height, -_height);           //画Y坐标
    painter.drawLine(0, -_height, _height, 0);           //画Y坐标
    // 绘制延长线
    painter.setPen(QPen(QColor(0, 255, 64), 5));
    const float pi = 3.1415926;
    const float circum = 3.1415926f*2;
    //const float Degree = 2*pi/divide + 0.01745f;   // pi/180=0.01745
    const float Degree = 2*pi/divide + 0.01f;   // pi/180=0.01745
    float degree;
    float angle = 2*pi/divide;
    float radii = ViewWH.height()/2;
    int Radii = ViewWH.height()/2;

    float py=0.0f;
    float px=0.0f;
    float ph=0.0f;
    float pw=0.0f;
    hh=(_height*1)/num;
    qDebug("\r\n[%s--%d] bar_len:%d ", __func__, __LINE__, bar_len);
    for(degree=0.0f; degree<Degree; degree +=0.01745f )
    {
        cnt=Bar_Cnt;
        for(i=0; i<divide; i++)
        {
            // 根据三角函数计算出以圆心为原点的等分圆的x,y坐标
            angle = (circum*i)/divide+degree;
            //qDebug("[%s--%d] divide:%d angle:%f circum:%f", __func__, __LINE__, divide, angle, circum);
            py = qSin(angle)*radii;
            px = qCos(angle)*radii;
            // 根据等比公式计算与大正方形的切线的高 ==> py/ph=px/radii ==> ph= py*radii/px
            ph = (py*radii)/px;
            // 根据等比公式计算与大正方形的切线的高 ==> pw/radii=px/py ==> pw= px*radii/py
            pw = (px*radii)/py;
            // -45°~+45°, <45 || > 315
            if(((circum-pi/4)<angle) || (angle<(pi/4)))
            {
                painter.drawLine(_height, -(Radii-(int)ph), Radii, -Radii);           //画Y坐标
                UG_DrawLine(_height/hh, (Radii-(int)ph)/hh, Radii/hh, Radii/hh, bar[cnt]);
                //OLED_DrawLine(_height/hh+1, (Radii-(int)ph)/hh, Radii/hh-1, Radii/hh+1, 1);
                //if((circum-pi/4)<angle)
                //{
                    //qDebug("[%s--%d] (%3d, %4d) (%3d, %4d)", __func__, __LINE__, _height, -(Radii-(int)ph), Radii, -Radii);
                    //qDebug("[%s--%d] (%3d, %4d) (%3d, %4d)", __func__, __LINE__, _height/hh+1, (Radii-(int)ph)/hh, Radii/hh-1, Radii/hh+1);
                //}
            }
            // +45°~+135°, >45 && < 315
            // NO.3 --------------
            if(((pi/4)<=angle) && (angle<(3*pi/4)))
            {
                painter.drawLine(Radii+(int)pw, 0, Radii, -Radii);           //画Y坐标
                UG_DrawLine((Radii+(int)pw)/hh, 0, Radii/hh, Radii/hh, bar[cnt]);
            }
            // +135°~+225°, >135 && < 225
            // NO.7 -------------
            if(((3*pi/4)<=angle) && (angle<(5*pi/4)))
            {
                painter.drawLine(0, -(Radii+(int)ph), Radii, -Radii);           //画Y坐标
                UG_DrawLine(0, (Radii+(int)ph)/hh, Radii/hh, Radii/hh, bar[cnt]);
                //qDebug("[%s--%d] (%3d, %4d) (%3d, %4d)", __func__, __LINE__, 0, (Radii+(int)ph)/hh, Radii/hh+1, Radii/hh);
            }
            // +225°~+315°, >225 && < 315
            // NO.12 ------------------------
            if(((5*pi/4)<=angle) && (angle<(7*pi/4)))
            {
                painter.drawLine(Radii-(int)pw, -_height, Radii, -Radii);           //画Y坐标
                UG_DrawLine((Radii-(int)pw)/hh, _height/hh, Radii/hh, Radii/hh, bar[cnt]);
                //qDebug("[%s--%d] (%3d, %4d) (%3d, %4d)", __func__, __LINE__, (Radii-(int)pw)/hh, _height/hh+1, Radii/hh, Radii/hh);
            }
            cnt++;
            if(cnt>=bar_len) cnt=0;
            //if(cnt<0) cnt=bar_len-1;
            //qDebug("[%s--%d] Radii:%d angle:%f (%f, %f) ph:%f %d %d", __func__, __LINE__, Radii, angle, px, py, ph, -(Radii-(int)ph), -(Radii-(int)py));
        }
    }
    painter.setBrush(QColor(187,229,253));
    painter.setPen(QPen(QColor(11,67,127),1));
    //painter.setPen(Qt::red); // setPen 设置轮廓线的颜色
    painter.setPen(QPen(Qt::green, 1));
    painter.setBrush(Qt::blue); // brush是画刷，是填充颜色
    //painter.drawRect(10, -10, 100, -400);
    //painter.drawEllipse(50, -150, 400, -200);
    /*for(i=0; i<num+1; i++)
    {
        hh=(_height*i)/num;
        painter.drawLine(0,-hh,_height,-hh);       //画X坐标
    }*/
    //OLED_DrawLine(0, 0, 10, 10, 1);
    //OLED_DrawLine(43, 1, 20, 22, 1);
    //OLED_DrawLine(41, 20, 20, 22, 1);
    //UG_DrawLine( 41, 41, 21, 21, 1);
    //oled_print(0, 32, 8, 1, "Watting...");
    hh=(_height*1)/num;
    //painter.drawRect(hh, -hh, hh, -hh);
    hh=(_height*1)/num;
    cnt=0;
    //color = bar[cnt];
    //lcd_pset(0, 1, color);
//    for(index=0; index<num; index++)
//    {
//        for(i=0; i<num+1; i++)
//        {
//            color = lcd_get_point(index, i);
//            if(color) // if(OLED_GetPoint(index, i))
//            {
//                painter.setBrush(QColor((color>>16)&0xFF, (color>>8)&0xFF, (color>>0)&0xFF));
//                //qDebug("[%s--%d] (%2d,%2d):%d \r\n", __func__, __LINE__, index, i, OLED_GetPoint(index, i));
//                //hh=(_height*i)/num;
//                //painter.drawLine(hh*index,-hh*i,hh*(index+1),-hh*(i+1));       //画X坐标
//                //painter.drawRect(hh*index,-hh*i,hh*(index+1),-hh*(i+1));
//                //painter.drawRect(hh*index, -hh*i, hh, hh);
//                //painter.drawRect(hh*(_height*index)/num, -hh*(_height*i)/num, hh, hh);
//                painter.drawRect((_height*index)/num, -(_height*i)/num, hh, hh);
//            }
//        }
//    }
    //painter.setBrush(QColor(187,229,253));
    //painter.setPen(QPen(QColor(11,67,127),1));
    //painter.drawRect(_height/2-_hh*10, -_height/2+_hh*2+10,_hh*21,-_hh*6); //窗口背景
}

void DrawDiagram::square3(uint16_t num, uint16_t divide)
{
    // 窗口大小
    const int _width=Widget->width();
    const int _height=Widget->height();

    uint32_t color, color_tmp;
    if(num<1) num=1;
    const int _ww = _width/num;
    const int _hh = _height/num;
    uint16_t hh=0;
    uint16_t cnt;
    //timeout();
    qDebug("[%s--%d] _width:[%d %d %d] _high:[%d %d %d] \r\n", __func__, __LINE__, _width, _ww, num, _height, _hh, num);
    int i, index;
    QPainter painter(Widget);
    painter.setPen(QPen(QColor(11,67,127),1));
    painter.setRenderHint(QPainter:: Antialiasing, true);  //设置渲染,启动反锯齿
    //QSize ViewWH(100,100);        //定义视口宽高
    QSize ViewWH(_height, _height);        //定义视口宽高
    painter.setViewport(0, 0,ViewWH.width(),ViewWH.height());
    //painter.setWindow(-50,50,ViewWH.width(),-ViewWH.height());
    painter.setWindow(-(_width/2-(_height/2)),0,ViewWH.width(),-ViewWH.height());
    painter.setBrush(QColor(187,229,253));
    painter.setPen(QPen(QColor(11,67,127),1));
    //painter.drawRect(-50,0,ViewWH.width(),-ViewWH.height()); //窗口背景
    //painter.drawRect(0,0,ViewWH.width(),-ViewWH.height()); //窗口背景
    painter.drawRect(_height/2-_hh*10, -_height/2+_hh*2+10,_hh*21,-_hh*6); //窗口背景
    //painter.drawLine(-50,-50,ViewWH.width(),-50);       //画X坐标
    //painter.drawLine(-50,-98,ViewWH.width(),-98);       //画X坐标
    //painter.drawLine(-50,_hh,_width,_hh);       //画X坐标
    //painter.drawLine(-50,_hh*2,_width,_hh*2);       //画X坐标
    for(i=0; i<num+1; i++)
    {
        hh=(_height*i)/num;
        painter.drawLine(0,-hh,_height,-hh);       //画X坐标
    }
    //painter.drawLine(0,-50,0,50);           //画Y坐标
    //painter.drawLine(70,-50,70,50);           //画Y坐标
    //painter.drawLine(10,-50,10,30);           //画Y坐标
    for(i=0; i<num+1; i++)
    {
        hh=(_height*i)/num;
        painter.drawLine(hh,-ViewWH.height(),hh,ViewWH.height());           //画Y坐标
    }
    painter.setPen(QPen(QColor(255, 0, 0), 3));
    //painter.drawLine(0, 0, (_height/2), -(_height/2));           //画Y坐标
    painter.drawLine(0, 0, _height, -_height);           //画Y坐标
    painter.drawLine(0, -_height, _height, 0);           //画Y坐标
    // 绘制延长线
    painter.setPen(QPen(QColor(0, 64, 32), 0));
    const float pi = 3.1415926;
    const float circum = 3.1415926f*2;
    //const float Degree = 2*pi/divide + 0.01745f;   // pi/180=0.01745
    const float Degree = 2*pi/divide + 0.01f;   // pi/180=0.01745
    float degree;
    float angle = 2*pi/divide;
    float radii = ViewWH.height()/2;
    int Radii = ViewWH.height()/2;

    float py=0.0f;
    float px=0.0f;
    float ph=0.0f;
    float pw=0.0f;
#if 1
    hh=(_height*1)/num;
    qDebug("\r\n[%s--%d] bar_len:%d ", __func__, __LINE__, bar_len);
    for(degree=0.0f; degree<Degree; degree +=(0.01745f) )
    {
        cnt=0;//Bar_Cnt;
        for(i=0; i<divide; i++)
        {
            // 根据三角函数计算出以圆心为原点的等分圆的x,y坐标
            angle = (circum*i)/divide+degree;
            //qDebug("[%s--%d] divide:%d angle:%f circum:%f", __func__, __LINE__, divide, angle, circum);
            py = qSin(angle)*radii;
            px = qCos(angle)*radii;
            // 根据等比公式计算与大正方形的切线的高 ==> py/ph=px/radii ==> ph= py*radii/px
            ph = (py*radii)/px;
            // 根据等比公式计算与大正方形的切线的高 ==> pw/radii=px/py ==> pw= px*radii/py
            pw = (px*radii)/py;
            // -45°~+45°, <45 || > 315
            if(((circum-pi/4)<angle) || (angle<(pi/4)))
            {
                painter.drawLine(_height, -(Radii-(int)ph), Radii, -Radii);           //画Y坐标
                UG_DrawLine(_height/hh, (Radii-(int)ph)/hh, Radii/hh, Radii/hh, bar[cnt]);
                //OLED_DrawLine(_height/hh+1, (Radii-(int)ph)/hh, Radii/hh-1, Radii/hh+1, 1);
                //if((circum-pi/4)<angle)
                //{
                    //qDebug("[%s--%d] (%3d, %4d) (%3d, %4d)", __func__, __LINE__, _height, -(Radii-(int)ph), Radii, -Radii);
                    //qDebug("[%s--%d] (%3d, %4d) (%3d, %4d)", __func__, __LINE__, _height/hh+1, (Radii-(int)ph)/hh, Radii/hh-1, Radii/hh+1);
                //}
            }
            // +45°~+135°, >45 && < 315
            // NO.3 --------------
            if(((pi/4)<=angle) && (angle<(3*pi/4)))
            {
                painter.drawLine(Radii+(int)pw, 0, Radii, -Radii);           //画Y坐标
                UG_DrawLine((Radii+(int)pw)/hh, 0, Radii/hh, Radii/hh, bar[cnt]);
            }
            // +135°~+225°, >135 && < 225
            // NO.7 -------------
            if(((3*pi/4)<=angle) && (angle<(5*pi/4)))
            {
                painter.drawLine(0, -(Radii+(int)ph), Radii, -Radii);           //画Y坐标
                UG_DrawLine(0, (Radii+(int)ph)/hh, Radii/hh, Radii/hh, bar[cnt]);
                //qDebug("[%s--%d] (%3d, %4d) (%3d, %4d)", __func__, __LINE__, 0, (Radii+(int)ph)/hh, Radii/hh+1, Radii/hh);
            }
            // +225°~+315°, >225 && < 315
            // NO.12 ------------------------
            if(((5*pi/4)<=angle) && (angle<(7*pi/4)))
            {
                painter.drawLine(Radii-(int)pw, -_height, Radii, -Radii);           //画Y坐标
                UG_DrawLine((Radii-(int)pw)/hh, _height/hh, Radii/hh, Radii/hh, bar[cnt]);
                //qDebug("[%s--%d] (%3d, %4d) (%3d, %4d)", __func__, __LINE__, (Radii-(int)pw)/hh, _height/hh+1, Radii/hh, Radii/hh);
            }
            cnt++;
            if(cnt>=bar_len) cnt=0;
            if(cnt<0) cnt=bar_len-1;
            //qDebug("[%s--%d] Radii:%d angle:%f (%f, %f) ph:%f %d %d", __func__, __LINE__, Radii, angle, px, py, ph, -(Radii-(int)ph), -(Radii-(int)py));
        }
    }
#else
    // NO.0
    // 根据三角函数计算出以圆心为原点的等分圆的x,y坐标
    angle = (circum*0)/divide;
    qDebug("[%s--%d] divide:%d angle:%f circum:%f", __func__, __LINE__, divide, angle, circum);
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> py/ph=px/radii ==> ph= py*radii/px
    ph = (py*radii)/px;
    painter.drawLine(_height, -(Radii-(int)ph), Radii, -Radii);           //画Y坐标
    qDebug("[%s--%d] Radii:%d angle:%f (%f, %f) ph:%f %d %d", __func__, __LINE__, Radii, angle, px, py, ph, -(Radii-(int)ph), -(Radii-(int)py));
    // NO.1
    // 根据三角函数计算出以圆心为原点的等分圆的x,y坐标
    angle = (circum*1)/divide;
    qDebug("[%s--%d] divide:%d angle:%f circum:%f", __func__, __LINE__, divide, angle, circum);
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> py/ph=px/radii ==> ph= py*radii/px
    ph = (py*radii)/px;
    painter.drawLine(_height, -(Radii-(int)ph), Radii, -Radii);           //画Y坐标
    //painter.drawLine(_height, -(_height/2-(int)py), (_height/2), -(_height/2));           //画Y坐标
    qDebug("[%s--%d] Radii:%d angle:%f (%f, %f) ph:%f %d %d", __func__, __LINE__, Radii, angle, px, py, ph, -(Radii-(int)ph), -(Radii-(int)py));
    // NO.2
    angle = (circum*2)/divide;
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> py/ph=px/radii ==> ph= py*radii/px
    ph = (py*radii)/px;
    //painter.drawLine(_height, -(int)ph, (_height/2), -(_height/2));           //画Y坐标
    painter.drawLine(_height, -(Radii-(int)ph), Radii, -Radii);           //画Y坐标
    //painter.drawLine(_height, -(_height/2-(int)py), (_height/2), -(_height/2));           //画Y坐标
    qDebug("[%s--%d] angle:%f (%f, %f) ph:%f %d %d", __func__, __LINE__, angle, px, py, ph, -(_height/2-(int)ph), -(_height/2-(int)py));
    // NO.3 --------------
    angle = (circum*3)/divide;
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> pw/radii=px/py ==> pw= px*radii/py
    pw = (px*radii)/py;
    //painter.drawLine((int)ph, 0, (_height/2), -(_height/2));           //画Y坐标
    painter.drawLine(Radii+(int)pw, 0, Radii, -Radii);           //画Y坐标
    qDebug("[%s--%d] angle:%f (%f, %f) ph:%f %d %d", __func__, __LINE__, angle, px, py, ph, -(_height/2-(int)ph), -(_height/2-(int)py));
    // NO.4
    angle = (circum*4)/divide;
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> pw/radii=px/py ==> pw= px*radii/py
    pw = (px*radii)/py;
    //painter.drawLine((int)ph, 0, (_height/2), -(_height/2));           //画Y坐标
    painter.drawLine(Radii+(int)pw, 0, Radii, -Radii);           //画Y坐标
    // NO.5
    angle = (circum*5)/divide;
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> pw/radii=px/py ==> pw= px*radii/py
    pw = (px*radii)/py;
    //painter.drawLine((int)ph, 0, (_height/2), -(_height/2));           //画Y坐标
    painter.drawLine(Radii+(int)pw, 0, Radii, -Radii);           //画Y坐标
    // NO.6
    angle = (circum*6)/divide;
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> pw/radii=px/py ==> pw= px*radii/py
    pw = (px*radii)/py;
    //painter.drawLine((int)ph, 0, (_height/2), -(_height/2));           //画Y坐标
    painter.drawLine(Radii+(int)pw, 0, Radii, -Radii);           //画Y坐标
    // NO.7 -------------
    angle = (circum*7)/divide;
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> py/ph=px/radii ==> ph= py*radii/px
    ph = (py*radii)/px;
    //painter.drawLine(_height, -(int)ph, (_height/2), -(_height/2));           //画Y坐标
    painter.drawLine(0, -(Radii+(int)ph), Radii, -Radii);           //画Y坐标
    qDebug("[%s--%d] angle:%f (%f, %f) ph:%f %d %d", __func__, __LINE__, angle, px, py, ph, -(_height/2-(int)ph), -(_height/2-(int)py));
    // NO.8
    angle = (circum*8)/divide;
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> py/ph=px/radii ==> ph= py*radii/px
    ph = (py*radii)/px;
    //painter.drawLine(_height, -(int)ph, (_height/2), -(_height/2));           //画Y坐标
    painter.drawLine(0, -(Radii+(int)ph), Radii, -Radii);           //画Y坐标
    // NO.9
    angle = (circum*9)/divide;
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> py/ph=px/radii ==> ph= py*radii/px
    ph = (py*radii)/px;
    //painter.drawLine(_height, -(int)ph, (_height/2), -(_height/2));           //画Y坐标
    painter.drawLine(0, -(Radii+(int)ph), Radii, -Radii);           //画Y坐标
    // NO.10
    angle = (circum*10)/divide;
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> py/ph=px/radii ==> ph= py*radii/px
    ph = (py*radii)/px;
    //painter.drawLine(_height, -(int)ph, (_height/2), -(_height/2));           //画Y坐标
    painter.drawLine(0, -(Radii+(int)ph), Radii, -Radii);           //画Y坐标
    // NO.11
    angle = (circum*11)/divide;
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> py/ph=px/radii ==> ph= py*radii/px
    ph = (py*radii)/px;
    //painter.drawLine(_height, -(int)ph, (_height/2), -(_height/2));           //画Y坐标
    painter.drawLine(0, -(Radii+(int)ph), Radii, -Radii);           //画Y坐标
    // NO.12 ------------------------
    angle = (circum*12)/divide;
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> pw/radii=px/py ==> pw= px*radii/py
    pw = (px*radii)/py;
    //painter.drawLine((int)ph, 0, (_height/2), -(_height/2));           //画Y坐标
    painter.drawLine(Radii-(int)pw, -_height, Radii, -Radii);           //画Y坐标
    // NO.13
    angle = (circum*13)/divide;
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> pw/radii=px/py ==> pw= px*radii/py
    pw = (px*radii)/py;
    //painter.drawLine((int)ph, 0, (_height/2), -(_height/2));           //画Y坐标
    painter.drawLine(Radii-(int)pw, -_height, Radii, -Radii);           //画Y坐标
    // NO.14
    angle = (circum*14)/divide;
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> pw/radii=px/py ==> pw= px*radii/py
    pw = (px*radii)/py;
    //painter.drawLine((int)ph, 0, (_height/2), -(_height/2));           //画Y坐标
    painter.drawLine(Radii-(int)pw, -_height, Radii, -Radii);           //画Y坐标
    // NO.15
    angle = (circum*15)/divide;
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> pw/radii=px/py ==> pw= px*radii/py
    pw = (px*radii)/py;
    //painter.drawLine((int)ph, 0, (_height/2), -(_height/2));           //画Y坐标
    painter.drawLine(Radii-(int)pw, -_height, Radii, -Radii);           //画Y坐标
    // NO.16
    angle = (circum*16)/divide;
    qDebug("[%s--%d] divide:%d angle:%f circum:%f", __func__, __LINE__, divide, angle, circum);
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> py/ph=px/radii ==> ph= py*radii/px
    ph = (py*radii)/px;
    painter.drawLine(_height, -(Radii-(int)ph), Radii, -Radii);           //画Y坐标
    // NO.17
    angle = (circum*17)/divide;
    qDebug("[%s--%d] divide:%d angle:%f circum:%f", __func__, __LINE__, divide, angle, circum);
    py = qSin(angle)*radii;
    px = qCos(angle)*radii;
    // 根据等比公式计算与大正方形的切线的高 ==> py/ph=px/radii ==> ph= py*radii/px
    ph = (py*radii)/px;
    painter.drawLine(_height, -(Radii-(int)ph), Radii, -Radii);           //画Y坐标
#endif
    //return ;
    painter.setBrush(QColor(187,229,253));
    painter.setPen(QPen(QColor(11,67,127),1));
    //painter.setPen(Qt::red); // setPen 设置轮廓线的颜色
    painter.setPen(QPen(Qt::green, 1));
    painter.setBrush(Qt::blue); // brush是画刷，是填充颜色
    //painter.drawRect(10, -10, 100, -400);
    //painter.drawEllipse(50, -150, 400, -200);
    /*for(i=0; i<num+1; i++)
    {
        hh=(_height*i)/num;
        painter.drawLine(0,-hh,_height,-hh);       //画X坐标
    }*/
    //OLED_DrawLine(0, 0, 10, 10, 1);
    //OLED_DrawLine(43, 1, 20, 22, 1);
    //OLED_DrawLine(41, 20, 20, 22, 1);
    //UG_DrawLine( 41, 41, 21, 21, 1);
    //oled_print(0, 32, 8, 1, "Watting...");
    hh=(_height*1)/num;
    //painter.drawRect(hh, -hh, hh, -hh);
    hh=(_height*1)/num;
    cnt=0;
    color = bar[cnt];
//    for(index=0; index<num; index++)
//    {
//        color = bar[cnt];
//        color_tmp = lcd_get_point(0, index);
//        if(0!=color_tmp)
//        {
//            cnt++;
//            if(cnt>=bar_len) cnt=0;
//            if(cnt<0) cnt=bar_len-1;
//            qDebug("[%s--%d] cnt:%d ", __func__, __LINE__, cnt);
//            continue;
//        }
//        for(i=21; i<num; i++)
//        {
//            color_tmp = lcd_get_point(index, i);
//            qDebug("[%s--%d] (%2d %2d) color_tmp:%06X ", __func__, __LINE__, index, i, color_tmp&0x00FFFFFF);
//            if(0==color_tmp)
//            {
//                lcd_pset(index, i, color);
//            }
//            else break;
//        }
//    }
    //lcd_pset(0, 1, color);
    for(index=0; index<num; index++)
    {
        for(i=0; i<num+1; i++)
        {
            color = ugui_get_point(index, i);
            if(color) // if(OLED_GetPoint(index, i))
            {
                painter.setBrush(QColor((color>>16)&0xFF, (color>>8)&0xFF, (color>>0)&0xFF));
                //qDebug("[%s--%d] (%2d,%2d):%d \r\n", __func__, __LINE__, index, i, OLED_GetPoint(index, i));
                //hh=(_height*i)/num;
                //painter.drawLine(hh*index,-hh*i,hh*(index+1),-hh*(i+1));       //画X坐标
                //painter.drawRect(hh*index,-hh*i,hh*(index+1),-hh*(i+1));
                //painter.drawRect(hh*index, -hh*i, hh, hh);
                //painter.drawRect(hh*(_height*index)/num, -hh*(_height*i)/num, hh, hh);
                painter.drawRect((_height*index)/num, -(_height*i)/num, hh, hh);
            }
        }
    }
}

void DrawDiagram::circle(uint16_t num, uint16_t divide)
{
    // 窗口大小
    const int _width=Widget->width();
    const int _height=Widget->height();

    uint32_t color;
    if(num<1) num=1;
    const int _ww = _width/num;
    const int _hh = _height/num;
    uint16_t hh=0;
    uint16_t cnt;
    //timeout();
    //qDebug("[%s--%d] _width:[%d %d %d] _high:[%d %d %d] \r\n", __func__, __LINE__, _width, _ww, num, _height, _hh, num);
    int i, index;
    QPainter painter(Widget);
    painter.setPen(QPen(QColor(11,67,127),1));
    painter.setRenderHint(QPainter:: Antialiasing, true);  //设置渲染,启动反锯齿
    //QSize ViewWH(100,100);        //定义视口宽高
    QSize ViewWH(_height, _height);        //定义视口宽高
    painter.setViewport(0, 0,ViewWH.width(),ViewWH.height());
    //painter.setWindow(-50,50,ViewWH.width(),-ViewWH.height());
    painter.setWindow(-(_width/2-(_height/2)),0,ViewWH.width(),-ViewWH.height());
    painter.setBrush(QColor(187,229,253));
    painter.setPen(QPen(QColor(11,67,127),1));
    //painter.drawRect(-50,0,ViewWH.width(),-ViewWH.height()); //窗口背景
    //painter.drawRect(0,0,ViewWH.width(),-ViewWH.height()); //窗口背景
    painter.drawRect(_height/2-_hh*10, -_height/2+_hh*2+10,_hh*21,-_hh*6); //窗口背景
    painter.setPen(QPen(QColor(0,0,0),10));
#if 1
    for(i=0; i<num+1; i++)
    {
        hh=(_height*i)/num;
        painter.drawLine(0,-hh,_height,-hh);       //画X坐标
    }
    for(i=0; i<num+1; i++)
    {
        hh=(_height*i)/num;
        painter.drawLine(hh,-ViewWH.height(),hh,ViewWH.height());           //画Y坐标
    }
#endif
    painter.setPen(QPen(QColor(255, 0, 0), 2));
    //painter.drawLine(0, 0, (_height/2), -(_height/2));           //画Y坐标
    painter.drawLine(0, 0, _height, -_height);           //画Y坐标
    painter.drawLine(0, -_height, _height, 0);           //画Y坐标
    struct draw_pixel center;   // 圆心坐标
    center.x = -_width/2;
    center.y = -_height/2;
    int radii = _width;         // 圆弧半径
    //if(radii>_height) radii=_height;
    radii=_hh*num+80;
    //radii -= 50;
    //radii -= 20;
    radii >>= 1;
    QRectF rectangle(center.x+radii+54, center.y-radii+2, (radii<<1), (radii<<1)); //位置矩形
    //QRectF rectangle(center.x, center.y, radii<<1, radii<<1); //位置矩形
    //painter.drawArc(rectangle,0*16,120*16); //绘画角度为30°~(30+120°)
    painter.drawPie(rectangle,0*16,359*16); //绘画角度为30°~(30+120°)
    // 绘制延长线
    painter.setPen(QPen(QColor(0, 64, 32), 0));
    struct matrix_pixel kb;
    //struct draw_pixel centre = {.y=(MATRIX_SIZE+1)/2, .x=(MATRIX_SIZE)/2};
    //return ;
    painter.setBrush(QColor(187,229,253));
    painter.setPen(QPen(QColor(11,67,127),1));
    //painter.setPen(Qt::red); // setPen 设置轮廓线的颜色
    painter.setPen(QPen(Qt::green, 1));
    painter.setPen(QPen(Qt::black, 0));
    painter.setBrush(Qt::blue); // brush是画刷，是填充颜色
    hh=(_height*1)/num;
    //painter.drawRect(hh, -hh, hh, -hh);
    hh=(_height*1)/num;
    cnt=0;
    color = bar[cnt];
    const struct matrix_pixel* pix;
    uint16_t angle;
    for(index=0; index<num; index++)
    {
        for(i=0; i<num+1; i++)
        {
            //kb.pixel = matrix[index][i].pixel;
            //color = ugui_get_point(index, i);
            //color = bar[kb.angle/divide];
            //color = angle_rg_gb_br(matrix[index][i].angle);
            pix = &matrix[index][i];
            angle = pix->angle;
#if 0
            color = (0xFF00)<<((angle&0x01)*8);
            //if(color) // if(OLED_GetPoint(index, i))
            if((pix->radius>64.3f) && (pix->radius<75.4f))
            {
                painter.setBrush(QColor((color>>16)&0xFF, (color>>8)&0xFF, (color>>0)&0xFF));
                //qDebug("[%s--%d] (%2d,%2d):%d \r\n", __func__, __LINE__, index, i, OLED_GetPoint(index, i));
                //hh=(_height*i)/num;
                //painter.drawLine(hh*index,-hh*i,hh*(index+1),-hh*(i+1));       //画X坐标
                //painter.drawRect(hh*index,-hh*i,hh*(index+1),-hh*(i+1));
                //painter.drawRect(hh*index, -hh*i, hh, hh);
                //painter.drawRect(hh*(_height*index)/num, -hh*(_height*i)/num, hh, hh);
                painter.drawRect((_height*index)/num, -(_height*i)/num, hh, hh);
            }
#endif
#if 1
            color = 0xAF00;
            //if(color) // if(OLED_GetPoint(index, i))
            if((pix->radius>=75.4f) && (pix->radius<76.1f))
            {
                painter.setBrush(QColor((color>>16)&0xFF, (color>>8)&0xFF, (color>>0)&0xFF));
                //qDebug("[%s--%d] (%2d,%2d):%d \r\n", __func__, __LINE__, index, i, OLED_GetPoint(index, i));
                //hh=(_height*i)/num;
                //painter.drawLine(hh*index,-hh*i,hh*(index+1),-hh*(i+1));       //画X坐标
                //painter.drawRect(hh*index,-hh*i,hh*(index+1),-hh*(i+1));
                //painter.drawRect(hh*index, -hh*i, hh, hh);
                //painter.drawRect(hh*(_height*index)/num, -hh*(_height*i)/num, hh, hh);
                painter.drawRect((_height*index)/num, -(_height*i)/num, hh, hh);
            }
#endif
#if 1
            color = 0x5F00;
            //if(color) // if(OLED_GetPoint(index, i))
            if((pix->radius>=76.1f) && (pix->radius<76.6f))
            {
                painter.setBrush(QColor((color>>16)&0xFF, (color>>8)&0xFF, (color>>0)&0xFF));
                //qDebug("[%s--%d] (%2d,%2d):%d \r\n", __func__, __LINE__, index, i, OLED_GetPoint(index, i));
                //hh=(_height*i)/num;
                //painter.drawLine(hh*index,-hh*i,hh*(index+1),-hh*(i+1));       //画X坐标
                //painter.drawRect(hh*index,-hh*i,hh*(index+1),-hh*(i+1));
                //painter.drawRect(hh*index, -hh*i, hh, hh);
                //painter.drawRect(hh*(_height*index)/num, -hh*(_height*i)/num, hh, hh);
                painter.drawRect((_height*index)/num, -(_height*i)/num, hh, hh);
            }
#endif
#if 1
            color = 0x2F00;
            //if(color) // if(OLED_GetPoint(index, i))
            if((pix->radius>=76.6f) && (pix->radius<76.99f))
            {
                painter.setBrush(QColor((color>>16)&0xFF, (color>>8)&0xFF, (color>>0)&0xFF));
                //qDebug("[%s--%d] (%2d,%2d):%d \r\n", __func__, __LINE__, index, i, OLED_GetPoint(index, i));
                //hh=(_height*i)/num;
                //painter.drawLine(hh*index,-hh*i,hh*(index+1),-hh*(i+1));       //画X坐标
                //painter.drawRect(hh*index,-hh*i,hh*(index+1),-hh*(i+1));
                //painter.drawRect(hh*index, -hh*i, hh, hh);
                //painter.drawRect(hh*(_height*index)/num, -hh*(_height*i)/num, hh, hh);
                painter.drawRect((_height*index)/num, -(_height*i)/num, hh, hh);
            }
#endif
#if 1
            color = 0x1F00;
            //if(color) // if(OLED_GetPoint(index, i))
            if((pix->radius>=76.99f) && (pix->radius<77.2f))
            {
                painter.setBrush(QColor((color>>16)&0xFF, (color>>8)&0xFF, (color>>0)&0xFF));
                //qDebug("[%s--%d] (%2d,%2d):%d \r\n", __func__, __LINE__, index, i, OLED_GetPoint(index, i));
                //hh=(_height*i)/num;
                //painter.drawLine(hh*index,-hh*i,hh*(index+1),-hh*(i+1));       //画X坐标
                //painter.drawRect(hh*index,-hh*i,hh*(index+1),-hh*(i+1));
                //painter.drawRect(hh*index, -hh*i, hh, hh);
                //painter.drawRect(hh*(_height*index)/num, -hh*(_height*i)/num, hh, hh);
                painter.drawRect((_height*index)/num, -(_height*i)/num, hh, hh);
            }
#endif
#if 1
            color = 0x0700;
            //if(color) // if(OLED_GetPoint(index, i))
            if((pix->radius>=77.2f) && (pix->radius<77.5f))
            {
                painter.setBrush(QColor((color>>16)&0xFF, (color>>8)&0xFF, (color>>0)&0xFF));
                //qDebug("[%s--%d] (%2d,%2d):%d \r\n", __func__, __LINE__, index, i, OLED_GetPoint(index, i));
                //hh=(_height*i)/num;
                //painter.drawLine(hh*index,-hh*i,hh*(index+1),-hh*(i+1));       //画X坐标
                //painter.drawRect(hh*index,-hh*i,hh*(index+1),-hh*(i+1));
                //painter.drawRect(hh*index, -hh*i, hh, hh);
                //painter.drawRect(hh*(_height*index)/num, -hh*(_height*i)/num, hh, hh);
                painter.drawRect((_height*index)/num, -(_height*i)/num, hh, hh);
            }
#endif
            //color = (0xFF00)<<((angle&0x01)*8);
            color = 0xFF00;
            //if(color) // if(OLED_GetPoint(index, i))
            if((pix->radius>64.3f) && (pix->radius<75.4f))
            {
                painter.setBrush(QColor((color>>16)&0xFF, (color>>8)&0xFF, (color>>0)&0xFF));
                //qDebug("[%s--%d] (%2d,%2d):%d \r\n", __func__, __LINE__, index, i, OLED_GetPoint(index, i));
                //hh=(_height*i)/num;
                //painter.drawLine(hh*index,-hh*i,hh*(index+1),-hh*(i+1));       //画X坐标
                //painter.drawRect(hh*index,-hh*i,hh*(index+1),-hh*(i+1));
                //painter.drawRect(hh*index, -hh*i, hh, hh);
                //painter.drawRect(hh*(_height*index)/num, -hh*(_height*i)/num, hh, hh);
                painter.drawRect((_height*index)/num, -(_height*i)/num, hh, hh);
            }
        }
    }
    //painter.drawPie(rectangle,0*16,120*16); //绘画角度为30°~(30+120°)
}

/**
(0,0)
-------->x+
\
\
\
y-
*/
void DrawDiagram::square(uint16_t num)
{
    // 窗口大小
    const int _width=Widget->width();
    const int _height=Widget->height();

    if(num<1) num=1;
    const int _ww = _width/num;
    const int _hh = _height/num;
    uint16_t hh=0;
    qDebug("[%s--%d] _width:[%d %d %d] _high:[%d %d %d] \r\n", __func__, __LINE__, _width, _ww, num, _height, _hh, num);
    int i;
    QPainter painter(Widget);
    painter.setPen(QPen(QColor(11,67,127),1));
    painter.setRenderHint(QPainter:: Antialiasing, true);  //设置渲染,启动反锯齿
    //QSize ViewWH(100,100);        //定义视口宽高
    QSize ViewWH(_height, _height);        //定义视口宽高
    painter.setViewport(0, 0,ViewWH.width(),ViewWH.height());
    //painter.setWindow(-50,50,ViewWH.width(),-ViewWH.height());
    painter.setWindow(-(_width/2-(_height/2)),0,ViewWH.width(),-ViewWH.height());
    painter.setBrush(QColor(187,229,253));
    painter.setPen(QPen(QColor(11,67,127),1));
    //painter.drawRect(-50,0,ViewWH.width(),-ViewWH.height()); //窗口背景
    //painter.drawRect(0,0,ViewWH.width(),-ViewWH.height()); //窗口背景
    //painter.drawLine(-50,-50,ViewWH.width(),-50);       //画X坐标
    //painter.drawLine(-50,-98,ViewWH.width(),-98);       //画X坐标
    //painter.drawLine(-50,_hh,_width,_hh);       //画X坐标
    //painter.drawLine(-50,_hh*2,_width,_hh*2);       //画X坐标
    for(i=0; i<num+1; i++)
    {
        hh=(_height*i)/num;
        painter.drawLine(0,-hh,_height,-hh);       //画X坐标
    }
    //painter.drawLine(0,-50,0,50);           //画Y坐标
    //painter.drawLine(70,-50,70,50);           //画Y坐标
    //painter.drawLine(10,-50,10,30);           //画Y坐标
    for(i=0; i<num+1; i++)
    {
        hh=(_height*i)/num;
        painter.drawLine(hh,-ViewWH.height(),hh,ViewWH.height());           //画Y坐标
    }
    painter.setPen(QPen(QColor(255, 0, 0), 3));
    //painter.drawLine(0, 0, (_height/2), -(_height/2));           //画Y坐标
    painter.drawLine(0, 0, _height, -_height);           //画Y坐标
    painter.drawLine(0, -_height, _height, 0);           //画Y坐标
}

// 画等分圆
void DrawDiagram::divide_circle(const uint16_t Divide)
{
    // 窗口大小
    const int _width=Widget->width();
    const int _height=Widget->height();
    // 等分数量
    uint16_t divide=Divide;
#if 0
    int hh = height();//-48;
    uint16_t angle = 30;
    QRectF rectangle(width()/2-(hh/2)-16, height()/2-(hh/2)+32, hh, hh); //位置矩形
#else
    uint16_t angle = 30;
    //QRectF rectangle(_width/2-(_height/2)-16, _height/2-(_height/2)-32, _height, _height); //位置矩形
    QRectF rectangle(_width/2-(_height/2), _height/2-(_height/2), _height, _height); //位置矩形
#endif
//    int startAngle = 30 * 16;     //起始角度,角度被分成了十六分之一
//    int spanAngle = 120 * 16;   //跨越度数
//    QPainter painter(this);painter.drawArc(rectangle, startAngle, spanAngle);
//    QRectF rectangle(10.0, 20.0, 80.0, 60.0);
//    int startAngle = 30 * 16;
//    int spanAngle = 30 * 16;

    QPainter painter(Widget);
    painter.setPen(QPen(QColor(11,67,127),3));
    painter.setRenderHint(QPainter:: Antialiasing, true);  //设置渲染,启动反锯齿
//    painter.drawPie(rectangle,30*16,120*16); //绘画角度为30°~(30+120°)
//    painter.drawPie(rectangle,150*16,120*16); //绘画角度为30°~(30+120°)
//    painter.drawPie(rectangle,270*16,120*16); //绘画角度为30°~(30+120°)
    painter.drawPie(rectangle,0*16,120*16); //绘画角度为30°~(30+120°)
    painter.drawPie(rectangle,120*16,120*16); //绘画角度为30°~(30+120°)
    painter.drawPie(rectangle,240*16,120*16); //绘画角度为30°~(30+120°)
    //painter.drawPie(rectangle, startAngle, spanAngle);
    painter.setPen(QPen(QColor(128,128,0),2));
    //painter.drawPie(rectangle,0*16,30*16);
    if(divide>360) divide=360;
    if(divide<1)   divide=1;
    angle = 360/divide;
    for(int i=0; i<divide; i++)
    {
        //painter.drawPie(rectangle,0*16,(30*(i+1))*16);
        painter.drawPie(rectangle,0*16,(angle*(i+1))*16);
    }
    //painter.setPen(QPen(QColor(128,0,0),8));
    //painter.drawPie(rectangle,0*16,(angle*(1+1))*16);
}

void DrawDiagram::divide_arc(const uint16_t Divide)
{
    // 窗口大小
    const int _width=Widget->width();
    const int _height=Widget->height();
    int radii = _width;         // 圆弧半径
    struct draw_pixel center;   // 圆心坐标
    center.x = _width/2;
    center.y = _height/2;
    if(radii>_height) radii=_height;
    radii >>= 1;
    // 等分数量
    uint16_t divide=Divide;
#if 0
    int hh = height();//-48;
    uint16_t angle = 30;
    QRectF rectangle(width()/2-(hh/2)-16, height()/2-(hh/2)+32, hh, hh); //位置矩形
#else
    uint16_t angle = 30;
    //QRectF rectangle(_width/2-(_height/2)-16, _height/2-(_height/2)-32, _height, _height); //位置矩形
    QRectF rectangle(center.x-radii, center.y-radii, radii<<1, radii<<1); //位置矩形
#endif
    // 圆弧的圆心为矩形中心
    QRectF arc1(center.x-radii, center.y, radii<<1, radii<<1); //位置矩形
    QRectF arc2(center.x, center.y-radii, radii<<1, radii<<1); //位置矩形
    QRectF arc3(center.x-radii, center.y-radii, radii<<1, radii<<1); //位置矩形
    QRectF arc4(center.x-radii, center.y-radii, radii<<1, radii<<1); //位置矩形

    QPainter painter(Widget);
    painter.setPen(QPen(QColor(11,67,127),3));
    painter.setRenderHint(QPainter:: Antialiasing, true);  //设置渲染,启动反锯齿
//    painter.drawPie(rectangle,30*16,120*16); //绘画角度为30°~(30+120°)
//    painter.drawPie(rectangle,150*16,120*16); //绘画角度为30°~(30+120°)
//    painter.drawPie(rectangle,270*16,120*16); //绘画角度为30°~(30+120°)
    painter.drawPie(rectangle,0*16,120*16); //绘画角度为30°~(30+120°)
    painter.drawPie(rectangle,120*16,120*16); //绘画角度为30°~(30+120°)
    painter.drawPie(rectangle,240*16,120*16); //绘画角度为30°~(30+120°)
    painter.drawPie(arc1,0*16,90*16);
    painter.drawPie(arc2,90*16,90*16);
    //painter.drawPie(rectangle, startAngle, spanAngle);
    painter.setPen(QPen(QColor(128,128,0),2));
    //painter.drawPie(rectangle,0*16,30*16);
    if(divide>360) divide=360;
    if(divide<1)   divide=1;
    angle = 360/divide;
    for(int i=0; i<divide; i++)
    {
        //painter.drawPie(rectangle,0*16,(30*(i+1))*16);
        painter.drawPie(rectangle,0*16,(angle*(i+1))*16);
    }
    //painter.setPen(QPen(QColor(128,0,0),8));
    //painter.drawPie(rectangle,0*16,(angle*(1+1))*16);
}
